
import React, { useEffect, useState } from 'react'
import { FormItem } from '../../components/VForm/interface'
import VForm from '../../components/VForm'
import { Button, DatePicker, Form, Select, message } from 'antd'
// import './index.css'
import { SumbitRepair } from './service'
import { getVehicleInfoList } from '../Statistics/VehicleStatusOverview/service'


export default function ReturnRequest() {
  const [form] = Form.useForm()
  const [vehicleInfo,setVehiclesInfoList] = useState([]);
  const filterOption = (input: string, option?: { label: string; value: string }) =>
  (option?.label ?? '').includes(input);
  const formItems: FormItem[] = [
 
    {
      name: 'vehicle_id',
      label: '车牌号',
      required: true,
      type:"custom",
      rule:{pattern:new RegExp('^[京津沪渝冀豫云辽黑湘皖闽鄂赣甘⽢桂贵琼陕青藏川宁新港澳台][A-HJ-NP-Z][·][A-HJ-NP-Z0-9]{5,6}$'),message:'请输入正确的车牌号，例如浙A·XD111'},
      customNode: vehicleInfo.length>0? <Select showSearch filterOption={filterOption} options={vehicleInfo.map((item:any)=>{
        return {label:item.vehicle_id,value:item.vehicle_id}
      })}   />: <></>
    }
    ,
    {
      name: 'return_date',
      label: '还车日期',
      required: true,
      type: 'custom',
      customNode: <DatePicker style={{width:"100%"}}/>,
    },

  ]
  const handleSumbit = async () => {
    const values=form.getFieldsValue();
    values.return_date=values.return_date.format('YYYY-MM-DD');
    /** 接口 */
    await SumbitRepair(values).then(res=>{
      if(res.data.result==="SUCCESS"){
        message.success('提交成功');
        form.resetFields();
      }
      else{
        message.error(res.data.message);
      }
    })
  }
  useEffect(() => {
    const queryList = async () => {
      await getVehicleInfoList().then(res => {
        setVehiclesInfoList(res.data.data)
      })
    }
    queryList();
  },[])
  return (
    <div className="Vform-container">
      <div className="Vform">
        <VForm formItems={formItems} form={form} />
      </div>

      <div className="sumbit-btn">
        <Button type="primary" onClick={handleSumbit}>
          提交
        </Button>
      </div>
    </div>
  )
}
